{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Project2 - Host program"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import time\n",
    "\n",
    "from pynq import Overlay\n",
    "import pynq.lib.dma\n",
    "from pynq import Xlnk\n",
    "\n",
    "ol = Overlay('phase.bit')\n",
    "\n",
    "# Download your bitstream to FPGA\n",
    "t_before_bitstream = time.time()\n",
    "ol.download()\n",
    "t_after_bitstream = time.time()\n",
    "\n",
    "print(t_after_bitstream - t_before_bitstream, 'seconds to program bitstream')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dmaIR = ol.streamPh.dma_I_R # First DMA\n",
    "dmaQT = ol.streamPh.dma_Q_T # Second DMA\n",
    "ph_ip = ol.streamPh.phasedetector # Your IP\n",
    "\n",
    "xlnk = Xlnk() # Contiguous Memory Allocator (CMA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "length = 1024\n",
    "\n",
    "# Open input/output files\n",
    "fI = open('input_i.dat','r')\n",
    "fQ = open('input_q.dat','r')\n",
    "fG = open('out_gold.dat', 'r')\n",
    "\n",
    "# Allocate regular numpy arrays to store input and output\n",
    "inp_I = np.empty([length,], dtype=np.float32)\n",
    "inp_Q = np.empty([length,], dtype=np.float32)\n",
    "golden_R = np.empty([length,], dtype=np.float32)\n",
    "golden_T = np.empty([length,], dtype=np.float32)\n",
    "\n",
    "# Store data into arrays\n",
    "for i in range (0, length):\n",
    "    golden_R[i], golden_T[i] = [np.float32(x) for x in next(fG).split()]\n",
    "    inp_I[i] = np.float32(next(fI))\n",
    "    inp_Q[i] = np.float32(next(fQ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(golden_R)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "plt.plot(golden_T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Golden thetas at the R peaks are:\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n\".format(golden_T[31],golden_T[63],golden_T[95],golden_T[127],golden_T[159],golden_T[191],golden_T[223],golden_T[255],golden_T[287],golden_T[319],golden_T[351],golden_T[383],golden_T[415],golden_T[447],golden_T[479],golden_T[511]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Complete the following block"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Allocate CMA array for DMA\n",
    "\n",
    "# Copy regular numpy arrays to CMA arrays\n",
    "\n",
    "# Write length using MMIO (we got the address from Vivado)\n",
    "\n",
    "t_start = time.time()\n",
    "# Begin data transfer from/to DMA\n",
    "t_stop = time.time()\n",
    "\n",
    "# Free the CMA buffers\n",
    "\n",
    "print(t_after_bitstream - t_before_bitstream, 'seconds to execute on hardware')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(out_bufferR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(out_bufferT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"Output thetas at the R peaks are:\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n {}\\n\".format(out_bufferT[31],out_bufferT[63],out_bufferT[95],out_bufferT[127],out_bufferT[159],out_bufferT[191],out_bufferT[223],out_bufferT[255],out_bufferT[287],out_bufferT[319],out_bufferT[351],out_bufferT[383],out_bufferT[415],out_bufferT[447],out_bufferT[479],out_bufferT[511]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
